package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.WorkReport;
import com.ruoyi.factorydata.service.IWorkReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报工记录Controller
 *
 * @author ruoyi
 * @date 2025-07-28
 */
@RestController
@RequestMapping("/workreport/workreport")
public class WorkReportController extends BaseController {
    @Autowired
    private IWorkReportService workReportService;

    /**
     * 查询报工记录列表
     */
    @PreAuthorize("@ss.hasPermi('workreport:workreport:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkReport workReport) {
        startPage();
        List<WorkReport> list = workReportService.selectWorkReportList(workReport);
        return getDataTable(list);
    }

    /**
     * 导出报工记录列表
     */
    @PreAuthorize("@ss.hasPermi('workreport:workreport:export')")
    @Log(title = "报工记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkReport workReport) {
        List<WorkReport> list = workReportService.selectWorkReportList(workReport);
        ExcelUtil<WorkReport> util = new ExcelUtil<WorkReport>(WorkReport.class);
        util.exportExcel(response, list, "报工记录数据");
    }

    /**
     * 获取报工记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('workreport:workreport:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(workReportService.selectWorkReportById(id));
    }


    @GetMapping("/getListByWorkOrderId/{workOrderId}")
    public AjaxResult getListByWorkOrderId(@PathVariable("workOrderId") Long workOrderId) {
        List<WorkReport> list = workReportService.getListByWorkOrderId(workOrderId);
        return success(list);
    }

    /**
     * 新增报工记录
     */
    @PreAuthorize("@ss.hasPermi('workreport:workreport:add')")
    @Log(title = "报工记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkReport workReport) {
        workReport.setCreateBy(getUserId().toString());
        return toAjax(workReportService.insertWorkReport(workReport));
    }

    /**
     * 修改报工记录
     */
    @PreAuthorize("@ss.hasPermi('workreport:workreport:edit')")
    @Log(title = "报工记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkReport workReport) {
        return toAjax(workReportService.updateWorkReport(workReport));
    }

    /**
     * 删除报工记录
     */
    @PreAuthorize("@ss.hasPermi('workreport:workreport:remove')")
    @Log(title = "报工记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workReportService.deleteWorkReportByIds(ids));
    }


    /**
     * 获取报工记录合计数据
     */
    @GetMapping("/getReportStatisticsSum")
    public AjaxResult getReportStatisticsSum(WorkReport workReport) {
        return success(workReportService.getReportStatisticsSum(workReport));
    }
}
